package com.linkage.educloud.ah.adapter.topic;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.TopicGroupBean;
import com.linkage.educloud.ah.util.ImageLoader;
import com.linkage.educloud.ah.view.activity.topic.TopicChildListActivity;
import com.zzhou.common.library.util.LogUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Mr'Tang on 2018/10/29.
 */

public class TopicGroupListAdapter extends XRecyclerView.Adapter<TopicGroupListAdapter.ViewHolder> {
    private List<TopicGroupBean> mlist;
    private Context mcontext;

    public TopicGroupListAdapter(Context context, List<TopicGroupBean> datas) {
        this.mcontext = context;
        this.mlist = datas;
    }

    public void addAll(List<TopicGroupBean> data, boolean append) {
        if (mlist != null) {
            if (!append) {
                mlist.clear();
            }
            mlist.addAll(data);
        } else {
            mlist = data;
        }
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_topic_group_list, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        ImageLoader.loadAvatar(mcontext, mlist.get(position).getImg(), viewHolder.item_topic_group_avater);
        viewHolder.item_topic_group_title.setText(mlist.get(position).getName());
        viewHolder.item_topic_group_content.setText(mlist.get(position).getContent());
        viewHolder.item_topic_group_card_num.setText(mlist.get(position).getCard_num());
        viewHolder.item_topic_group_today_num.setText(mlist.get(position).getToday_num());
        viewHolder.item_topic_group_browse_num.setText(mlist.get(position).getBrowse_num());

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TopicChildListActivity.launch(mcontext, mlist.get(position).getName(), mlist.get(position).getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        CircleImageView item_topic_group_avater;
        TextView item_topic_group_title;
        TextView item_topic_group_content;
        TextView item_topic_group_card_num;
        TextView item_topic_group_today_num;
        TextView item_topic_group_browse_num;

        public ViewHolder(View convertView) {
            super(convertView);
            item_topic_group_avater = convertView.findViewById(R.id.item_topic_group_avater);
            item_topic_group_title = convertView.findViewById(R.id.item_topic_group_title);
            item_topic_group_content = convertView.findViewById(R.id.item_topic_group_content);
            item_topic_group_card_num = convertView.findViewById(R.id.item_topic_group_card_num);
            item_topic_group_today_num = convertView.findViewById(R.id.item_topic_group_today_num);
            item_topic_group_browse_num = convertView.findViewById(R.id.item_topic_group_browse_num);
        }
    }
}